<template>
  <div class="c-tree-node" >
    <div class="_vti-line" :class="{_root:isRoot}" ></div>
    <div v-if="isSingle" class="_single-vt-line"></div>
    <span class="g-relative g-one-line">
      <span v-if="showExpandIcon" class="g-pointer" @click="handleToggleExpand">
        <div class="_rect">
          <div  class="_rect-1"></div>
          <div v-if="!nodeData.expand" class="_rect-2"></div>
        </div>
      </span>
      <span v-else class="_hr-line _empty-line"></span>
      <span class="_hr-line" />
      <m-node-content :nodeData="nodeData" @click.native.stop="handleClickNode"/>

    </span>
    <div v-if="nodeData.children && nodeData.children && nodeData.expand" class="_sub-node-wrapper">
      <m-tree-node
        v-for="(item,index) in sortedList"
        :key="item[tree.nodeKey]"
        :nodeData="item"
        :index="index"
        :is-single="sortedList.length===1"  
      />
     
    </div> 
     
  </div>
</template>
<script>
import { GraphNodeUtil } from '@/pages/mxgraph/GraphNodeUtil';

const NodeContent = {
  props: {
    nodeData: { ////
      required: true
    }
  },
  render(h) {
    const parent = this.$parent;
    const tree = parent.tree;
    const nodeData = this.nodeData;
    //// const { data, store } = node;
    return (
      tree.$scopedSlots.default
        ? tree.$scopedSlots.default({ nodeData })
        : <span >{ nodeData.label }</span>
    );
  }
};

export default {
  name: "m-tree-node",
  inject: ['tree'],
  components: {
    "m-node-content": NodeContent
  },
  props: {
    nodeData: Object,
    index: Number,
    isSingle: Boolean,
    isRoot: Boolean,
    showExtraFile: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {};
  },
  computed: {
    sortedList(){
      if (!this.tree.sortByName) return this.nodeDataChildren;
      console.log('sort');
      this.nodeDataChildren.forEach(child => {
        child._displayTitle = child.displayTitle;
      });
      return [...this.nodeDataChildren].sort((i, j) => {
        const orderi =  this.getOrder(i);
        const orderj =  this.getOrder(j);
        if (orderi === orderj){
          return (i._displayTitle || '').localeCompare(j._displayTitle || '');

        } else {
          return orderi - orderj;
        }
        //// if (i.getType() === 'Relations') return -1;

      });
    },
    showExpandIcon(){
      if (this.tree.lazy){
        return !this.nodeData.isLeaf;
      } else {
        return this.nodeDataChildren && this.nodeDataChildren.length;
      }
    },
    nodeDataChildren() {
      if (this.showExtraFile) {
        return this.nodeData.children;
      } else {
        return this.nodeData.children.filter(child => !(child.disabled && child.getTypeName() === 'Profile'));
      }
    }

  },
  mounted() {
    
  },
  methods: {
    handleToggleExpand() {
      this.tree.toggleExpand( this.nodeData);

    },
    handleClickNode(){
      this.tree.setCurrentNode(this.nodeData);
    },
    sortNode(i, j){
      if (i.getTypeName() === 'Relations') return -1;

    },
    getOrder(node){
      if (node.getTypeName() === 'Relations') return 1;
      const typeName = node.getTypeName();
      if (GraphNodeUtil.isOrIsInstanceOf(typeName, "Package")){
        return 2;
      }
      if (typeName === 'Metaclass') return 3;
      //// if (GraphNodeUtil.isInstanceOf(typeName, "IPackage")){
      ////   return 1;
      //// }

      return 100;
      //// if (node.getTypeName;
      //// if()() === 'relationGroup') return 0;

    }
  },
};
</script>
<style lang="scss">
.c-tree-node {
	position: relative;
  line-height: 18px;
	._sub-node-wrapper {
		margin-left: 27px;
		position: relative;
	}
	&:last-of-type {
		& > ._vti-line {
			display: none;
		}
	}
	& > ._vti-line {
		height: calc(100% + 11px);
    border-left: 1px solid black;
    position: absolute;
    top: -1px;
    left: 5px;
    &._root {
      height: calc(100% + 1px);
      top: 9px;
       
    }
  }
  & > ._single-vt-line {
    height: 12px;
    border-left: 1px solid black;
    position: absolute;
    top: -2px;
    left: 6px;

  }
  ._hr-line {
    display: inline-block;
    width: 12px;
    height: 1px;
    border-top: 1px solid black;
    position: relative;
    top: -2px;
    &._empty-line {
      margin-left: 5.5px;
      width:5px
    }
  }
  ._rect {
    width: 11px;
    height: 11px;
    box-sizing: border-box;
    border: 1px solid black;
    position: relative;
    display: inline-block;
    background: white;
    top: 2px;
    ._rect-1 {
      position: absolute;
      width: 5px;
      border-top: 1px solid black;
      left: 2px;
      top: 4px;

    }
    ._rect-2 {
      position: absolute;
      height: 5px;
      border-left: 1px solid black;
      left: 4px;
      top: 2px;

    }
  }
}
</style>